﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.Preferences;
using ApplicationLibrary.SqlConnector;
using Plumber.Accounting.ModuleData;
using Plumber.Accounting.Preferences;
using Plumber.Masters.ModuleData;
using Plumber.Sales.ModuleData;

namespace Plumber.Accounting.Scrounger
{
    internal class MasterScrounger : ApplicationLibrary.DBManager.Scrounger
    {
        private const string JournalEntrySlave = "JOURNALENTRSLAVE";

        private UserTabControl<TabJE> UtctabJE = null;
        private TabJE tabJE = null;
        private Masters.Forms.Masters frm;
        private PreferencesManager<AccountingPref> myAccountingPref = null;
        private AccountingPref AccountingPref = null;

        public MasterScrounger(iScrounger frm)
            : base(frm)
        {
            this.frm = (Masters.Forms.Masters)frm;

            myAccountingPref = new PreferencesManager<AccountingPref>(this.frm.Name, null);
            AccountingPref = myAccountingPref.ReadPreference();
        }

        public override void InitializerComponent()
        {
            if (AccountingPref.EnableLink)
            {
                UtctabJE = new UserTabControl<TabJE>(Properties.Resources.TD_JournalEntries);
                UtctabJE.Name = "tpgJE";

                frm.masterTabControl.Controls.Add(UtctabJE);
                tabJE = UtctabJE.UserControl;
                frm.CbbType.SelectedValueChanged += new EventHandler(CbbType_SelectedValueChanged);
            }
            base.InitializerComponent();
        }

        private void CbbType_SelectedValueChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(frm.CbbType.GetValue<string>()))
            {
                CodeMode cm = frm.CbbType.dRMasterType.GetValue<CodeMode>(PL_MasterType.CodeMode);
                MasterTypes mt = frm.CbbType.dRMasterType.GetValue<MasterTypes>(PL_MasterType.MasterType);
                ManageTabJE(frm.CbbType.dRMasterType.GetValue<bool>(PL_MasterType.CanReceiveInvoice));
            }
        }

        private void ManageTabJE(bool visible)
        {
            TabPage tp = frm.masterTabControl.TabPages["tpgJE"];
            if (tp == null)
            {
                if (visible)
                    frm.masterTabControl.TabPages.Add(UtctabJE);
            }
            else
            {
                if (!visible)
                    frm.masterTabControl.TabPages.Remove(UtctabJE);
            }
        }

        public override void OnAttachData(DBManager dbManager)
        {
            if (AccountingPref.EnableLink)
            {
                dbManager.AddSlave(JournalEntrySlave, false);
                tabJE.OnAttachData(dbManager);
            }
            base.OnAttachData(dbManager);
        }

        public override void OnPrepareAuxData()
        {
            if (AccountingPref.EnableLink)
                tabJE.AutoResizeColumns();

            tabJE.CalculateTotal();

            base.OnPrepareAuxData();
        }

        public override void OnBindData()
        {
            if (AccountingPref.EnableLink)
                tabJE.OnBindData(frm);

            base.OnBindData();
        }

        public override string CreateSlaveQuery(string name)
        {
            QueryBuilder qb = new QueryBuilder();
            if (name == JournalEntrySlave)
            {
                qb.SetQualified = true;
                qb.AddSelect();
                qb.AddFrom(PL_JournalEntryDetail.Name);
                qb.AddJoin(QueryBuilder.JoinType.INNER, PL_JournalEntry.JournalNo, PL_JournalEntryDetail.JournalNo);
                qb.AddCompare(PL_JournalEntryDetail.Type, "@p4");
                qb.AddCompare(PL_JournalEntryDetail.Code, "@p5");
                qb.Where += string.Format(" AND ({0} BETWEEN @p6 AND  @p7 OR {1} = '0')", PL_JournalEntry.PostingDate.Name,
                    PL_JournalEntryDetail.Close.Name);
            }
            return qb.Query;
        }

        public override List<SqlABParameter> CreateSlaveParam(string name)
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();
            if (name == JournalEntrySlave)
            {
                SqlABParameter p1 = new SqlABParameter("@p4", PL_SalesInvoices.Type);
                p1.Value = "";
                PList.Add(p1);
                SqlABParameter p2 = new SqlABParameter("@p5", PL_SalesInvoices.Master);
                p2.Value = "";
                PList.Add(p2);
                SqlABParameter p3 = new SqlABParameter("@p6", PL_SalesInvoices.ReleaseDate);
                p3.Value = DateTime.Today;
                PList.Add(p3);
                SqlABParameter p4 = new SqlABParameter("@p7", PL_SalesInvoices.ReleaseDate);
                p4.Value = DateTime.Today;
                PList.Add(p4);
            }
            return PList;
        }

        public override void SetParameters(DBManager dbManager, IRadarParameters key, DBCollection collection)
        {
            if (collection.Name == JournalEntrySlave)
            {
                collection.Parameter[0].Value = key[1];
                collection.Parameter[1].Value = key[0];
                collection.Parameter[2].Value = new DateTime(GlobalInfo.CurrentDate.Year - 1, 1, 1);
                collection.Parameter[3].Value = new DateTime(GlobalInfo.CurrentDate.Year, 12, 31);
            }
            base.SetParameters(dbManager, key, collection);
        }
    }
}